import 'package:aichat_flutter_wallet/kai_print.dart';
import 'package:aichat_flutter_wallet/module/module_integral/model/integral_api.dart';
import 'package:aichat_flutter_wallet/module_common/utils/colors.dart';
import 'package:aichat_flutter_wallet/module_common/widgets/header/page_header.dart';
import 'package:aichat_flutter_wallet/module_common/widgets/page_status_com.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:lib_util/utils/utils.dart';

class IntegralDetailPage extends StatefulWidget {
  IntegralDetailPage({
    super.key,
  });

  @override
  State<IntegralDetailPage> createState() => _IntegralDetailPageState();
}

class _IntegralDetailPageState extends State<IntegralDetailPage> {
  ScrollController _scrollController = new ScrollController(); //监听下拉框的状态
  List<Map<String, dynamic>> integralRecord = [];
  int page = 1;
  int size = 15;
  int count = -1;
  bool isLoad = false;

  String integralType(int type) {
    switch (type) {
      case 1:
        return 'Sign In Task';
      case 2:
        return 'Beginner Tasks -';
      case 3:
        return 'Daily Tasks -';
      case 4:
        return 'Advanced Tasks -';
      default:
        return '';
    }
  }

  Widget DetailList(BuildContext context, List<Map<String, dynamic>> list) {
    return ListView.builder(
        padding: EdgeInsets.fromLTRB(20.w, 15.w, 20.w, 10.w + MediaQuery.of(context).padding.bottom),
        itemCount: list.length + 1,
        controller: _scrollController,
        itemBuilder: (BuildContext buildContext, int index) {
          if (index == list.length) {
            return Container(
              padding: EdgeInsets.symmetric(vertical: 20.w),
              child: Text(
                'Already at the bottom',
                textAlign: TextAlign.center,
                style: TextStyle(color: AppColors.c_BEBDCA, fontSize: 12.sp, fontWeight: FontWeight.w400),
              ),
            );
          } else {
            Map<String, dynamic> item = list[index];
            return Container(
              padding: EdgeInsets.symmetric(horizontal: 15.w),
              decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.only(
                      topLeft: index == 0 ? Radius.circular(10.w) : Radius.zero,
                      topRight: index == 0 ? Radius.circular(10.w) : Radius.zero,
                      bottomLeft: index == (list.length - 1) ? Radius.circular(10.w) : Radius.zero,
                      bottomRight: index == (list.length - 1) ? Radius.circular(10.w) : Radius.zero)),
              child: Container(
                width: double.infinity,
                padding: EdgeInsets.only(top: 15.w, bottom: 15.w),
                decoration: BoxDecoration(
                    border: Border(
                        bottom: BorderSide(color: AppColors.c_F6F6F6, width: list.length - 1 == index ? 0 : 1.w))),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Expanded(
                      flex: 1,
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Text(
                            integralType(item['task_type']) + (item['task_type'] == 1 ? '' : item['task_name']),
                            style: TextStyle(color: AppColors.c_333333, fontSize: 14.sp, fontWeight: FontWeight.w400),
                          ),
                          SizedBox(
                            height: 8.w,
                          ),
                          Text(
                            Utils.formatDateTime(int.parse(item['created_at'].toString())),
                            style: TextStyle(color: AppColors.c_BEBDCA, fontSize: 14.sp, fontWeight: FontWeight.w400),
                          )
                        ],
                      ),
                    ),
                    SizedBox(
                      width: 20.w,
                    ),
                    Text(
                      '+${item['integral']}',
                      style: TextStyle(
                          fontFamily: '_DIN', fontWeight: FontWeight.w700, color: AppColors.c_8667FE, fontSize: 16.sp),
                    )
                  ],
                ),
              ),
            );
          }
        });
  }

  @override
  void initState() {
    super.initState();
    getIntegralRecord();
    //触底加载
    _scrollController.addListener(() {
      if (_scrollController.position.pixels == _scrollController.position.maxScrollExtent) {
        getIntegralRecord();
      }
    });
  }

  Future getIntegralRecord() async {
    try {
      if (isLoad || (integralRecord.length >= count && count != -1)) return;
      isLoad = true;
      Map<String, dynamic> data = await IntegralApi.getIntegralRecord(page: page, size: size);
      if (mounted) {
        setState(() {
          integralRecord = [...integralRecord, ...data['record_list']];
        });
      }
      count = int.parse(data['count'].toString());
      isLoad = false;
      page++;
    } catch (e) {
      kaiPrint(e);
    }
  }

  @override
  Widget build(BuildContext context) {
    List<Map<String, dynamic>> a = List<Map<String, dynamic>>.filled(20, {});
    return Scaffold(
      body: Container(
        height: double.infinity,
        decoration: BoxDecoration(
            image: DecorationImage(image: AssetImage("assets/images/integral/detail_bg.png"), fit: BoxFit.cover),
            color: AppColors.c_F6F6F6),
        child: Column(children: [
          PageHeaderCom(title: 'Points Details', bgColor: Colors.transparent, showBottomBorder: false),
          Expanded(
            flex: 1,
            child: integralRecord.length == 0
                ? PageEmptyCom(type: 'empty', paddingTop: 100.w)
                : DetailList(context, integralRecord),
          )
        ]),
      ),
    );
  }
}
